﻿layui.use([ 'layer',   'jquery',   'upload'], function () {
    var layer = layui.layer;
    var upload = layui.upload;
    var $ = layui.jquery;

    //单文件上传
    upload.render({
        elem: '#batchImport',
        accept: 'file',
        exts: 'xlsx|xls',
        url: 'http://localhost:8080/book/batchImportBook',
        auto: false,
        bindAction: '#submitForm',
        before: function (obj) {
            layer.load(3);
        },
        //执行上传动作
        done: function (data) {
            console.log(data);
            if (data.code == 1) {
                layer.open({
                    closeBtn: 0,
                    btn: ['关闭'],
                    content: '<div style="color: #00B83F"> 导入全部成功 </div>',
                    yes: function () {
                        layer.closeAll();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    },
                    end: function () {
                        window.parent.location.reload();
                    }
                })
            } else {
                layer.alert(data.msg, {closeBtn: 0}, function () {
                    layer.closeAll();
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    window.parent.location.reload();
                })
            }
        },
    });
    /*  form.on("submit(submitForm)", function (data) {
          uploadInst.config.url += data.field.distributionId;
          uploadInst.upload();
      })*/
    //下载模板
    $("#downTemplate").on('click', function () {
        var url = "http://localhost:8080/book/downTemplate?";
        var fileName = "批量导入模板.xlsx";
        progressDownLoad(url, fileName, function () {
        }, function () {
        });
    });
    //清空
    $("#clearForm").click(function () {
        /*parent.layer.close(parent.layer.getFrameIndex(window.name));*/
        //刷新该页面
        window.location.reload();
    });
    //下载进程解析
    function progressDownLoad(url,filename,progress,success){
        var xhr = new XMLHttpRequest();
        xhr.open("POST", url, true);
        //监听进度事件
        xhr.addEventListener("progress", function (evt) {
            if(progress) try{ progress.call(evt); }catch(e){}
        }, false);

        xhr.responseType = "blob";
        xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (typeof window.chrome !== 'undefined') {
                    // Chrome version
                    var link = document.createElement('a');
                    link.href = window.URL.createObjectURL(xhr.response);
                    link.download = filename;
                    link.click();
                } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
                    // IE version
                    var blob = new Blob([xhr.response], { type: 'application/force-download' });
                    window.navigator.msSaveBlob(blob, filename);
                } else {
                    // Firefox version
                    var file = new File([xhr.response], filename, { type: 'application/force-download' });
                    window.open(URL.createObjectURL(file));
                }
                if(success) try{ success.call(xhr); }catch(e){}
            }
        };
        xhr.send(url);
    }
})